package com.zgl.modules.order.controller;

import com.zgl.modules.card.DTO.CardDTO;
import com.zgl.modules.goods.entity.GoodsEntity;
import com.zgl.modules.goods.service.Impl.GoodsServiceImpl;
import com.zgl.modules.order.DTO.OrderDTO;
import com.zgl.modules.order.entity.OrderEntity;
import com.zgl.modules.order.service.OrderService;
import com.zgl.modules.order.service.impl.OrderServiceImpl;
import com.zgl.modules.user.entity.User;
import com.zgl.modules.user.service.UserService;
import com.zgl.modules.user.service.impl.UserServiceImpl;
import com.zgl.modules.wallet.entity.WalletEntity;
import com.zgl.modules.wallet.service.impl.WalletServiceImpl;
import com.zgl.utils.Params;
import com.zgl.utils.R;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Api(tags = "订单操作")
@Controller
@RequestMapping("/order/")
public class OrderController {
    @Resource
    private OrderServiceImpl orderService;
    @Resource
    private UserServiceImpl userService;
    @Resource
    private GoodsServiceImpl goodsService;
    @Resource
    private WalletServiceImpl walletService;
    @ResponseBody
    @PostMapping("/createOrder")
    @ApiOperation(value = "生成订单")
    public R createOrder(@RequestBody Params<OrderDTO> params){
        return orderService.createOrder(params.getParams());
    }


    @ResponseBody
    @PostMapping("/deleteOrder")
    @ApiOperation(value = "用户使用删除订单")
    public R deleteOrder(@RequestBody Params<OrderDTO> params){
        //将删除属性设为1
        return orderService.deleteOrder(params.getParams());
    }
    @ResponseBody
    @PostMapping("/queryOorderByName")
    @ApiOperation(value = "根据商品名称查询商品")
    public R queryOorderByName(@RequestBody OrderDTO orderDTO){
        //将删除属性设为1
        return orderService.queryOorderByName(orderDTO);
    }
    @ResponseBody
    @PostMapping("/sellProducts")
    @ApiOperation(value = "出售产品")
    public R sellProducts(@RequestBody Params<OrderDTO> params){
        return orderService.sellProducts(params.getParams());
    }
    @ResponseBody
    @PostMapping("/queryOrderByUserId")
    @ApiOperation(value = "根据用户查询订单")
    public R queryOrderByUserId(@RequestBody Params<OrderDTO> params){
        final String headerToken = params.getParams().getToken();
        Claims claims = Jwts.parser().setSigningKey("zgl-key").parseClaimsJws(headerToken).getBody();
        String tokenUserId = (String) claims.get("userid");
        int itokenUserId=Integer.parseInt(tokenUserId);
        return  orderService.getOrderByUserId(itokenUserId,params);
    }


}
